package com.train.course.controller;

import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.course.dto.HistoryReportDTO;
import com.train.course.service.IVideoViewHistoryService;
import com.train.course.vo.HistoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频观看历史管理 控制器
 */
@RestController
@RequestMapping("/api/v1/history")
public class VideoViewHistoryController extends BaseController {

    @Autowired
    private IVideoViewHistoryService videoViewHistoryService;

    /**
     * 上报观看进度
     */
    @PostMapping("/report")
    public AjaxResult reportProgress(@RequestBody HistoryReportDTO dto) {
        Long userId = getUserId();
        boolean result = videoViewHistoryService.reportProgress(userId, dto.getEpisodeId(), dto.getProgress());
        return result ? success("进度上报成功") : error("进度上报失败");
    }

    /**
     * 查询观看历史列表（下滑刷新）
     */
    @GetMapping("/list")
    public AjaxResult getHistoryList(@RequestParam(value = "lastHistoryId", defaultValue = "0") Long lastHistoryId,
                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        Long userId = getUserId();
        List<HistoryVO> historyList = videoViewHistoryService.getHistoryList(userId, lastHistoryId, pageSize);
        return success(historyList);
    }
}
